package com.kl.test;

import com.kl.bean.User;
import com.kl.utils.ClassScanner;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.Set;
import java.util.function.Predicate;

public class Main {
    public static void main(String[] args) {
        Field[] declaredFields = User.class.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField.getType().getSimpleName());
        }
    }

    @Test
    public void test1() throws IOException {
        // 实例化一个File对象。参数不同时，获取的最终结果也不同, 这里可以将 path 替换为要扫描的包路劲 例如 org/example
        String path = "com/kl/bean";
        File directory = new File(path);
        // 获取标准路径。该方法需要放置在try/catch块中，或声明抛出异常---/Users/superlkl/IdeaProjects/middleware_study/jsonschema/com/kl/bean
        System.out.println(directory.getCanonicalPath());
        // 获取绝对路径---/Users/superlkl/IdeaProjects/middleware_study/jsonschema/com/kl/bean
        System.out.println(directory.getAbsolutePath());
    }

    @Test
    public void test2() throws IOException {
        Enumeration<URL> resources = Thread.currentThread().
                getContextClassLoader().getResources("com/kl/bean");
        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            //---file:/Users/superlkl/IdeaProjects/middleware_study/jsonschema/target/classes/com/kl/bean
            System.out.println(url.toString());
        }
    }

    @Test
    public void testGetPackageAllClasses() throws IOException, ClassNotFoundException {
        Predicate<String> packagePredicate = s -> true;
        ClassScanner scanner = new ClassScanner("com.kl.bean",
                true, packagePredicate, null);
        Set<Class<?>> packageAllClasses = scanner.doScanAllClasses();
        packageAllClasses.forEach(it -> {
            System.out.println("扫描到的类===》"+it.getName());
            try {
                Class<?> aClass = Class.forName(it.getName());
                Field[] declaredFields = aClass.getDeclaredFields();
                for (Field declaredField : declaredFields) {
                    System.out.println(declaredField.getType().getSimpleName());
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            System.out.println("=========");
        });
    }
}
